import requests
from parsel import Selector
from PyQt5.QtCore import *


class CrawlThread(QThread):
    finish_signal = pyqtSignal()
    log_signal = pyqtSignal(str)
    data_signal = pyqtSignal(list)

    def __init__(self, window):
        super().__init__()
        self.window = window
        self.flag = True

    def run(self):
        page_count = 0
        total_page = self.window.page_spin_box.value()

        self.flag = True
        while page_count < total_page:
            if self.flag:
                page_count += 1
                self.crawl_page(page_count)
            else:
                break
        self.finish_signal.emit()

    def crawl_page(self, page_num):
        self.log_signal.emit(f"当前正在爬取第{page_num}页")
        page_url = f"https://quotes.toscrape.com/page/{page_num}/"

        try:
            response = requests.get(page_url)
            response.raise_for_status()  # 检查请求是否成功

            if "No quotes found!" in response.text:
                self.log_signal.emit(f"当前页面没有名言了，不再继续爬取")
                return

            selector = Selector(response.text)
            quotes = selector.xpath('//div[@class="quote"]')
            for quote in quotes:
                content = quote.xpath('./span/text()').extract_first()
                author = quote.xpath('./span/small/text()').extract_first()
                tags = quote.xpath('./div[@class="tags"]/a/text()').extract()
                tags = ','.join(tags)
                self.data_signal.emit([content, author, tags])
        except requests.RequestException as e:
            self.log_signal.emit(f"请求第{page_num}页时出错: {str(e)}")
        except Exception as e:
            self.log_signal.emit(f"处理第{page_num}页时出错: {str(e)}")

    def stop(self):
        self.flag = False
